package panel.comment
{
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.TextEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.utils.setTimeout;
	
	import assists.ButtonUtils;
	import assists.HtmlText;
	import assists.Lang;
	import assists.SizeManager;
	import assists.TextFieldUtils;
	import assists.URI;
	import assists.resource.ResourceLoader;
	
	import inerface.ICommentPanel;
	
	import main.ClientConstants;
	
	import panel.stars.StarGroup;
	
	public class CommentPanel extends Sprite implements ICommentPanel
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		//=========组件
		private var titleText:TextField;
		private var star:StarGroup;
		private var commentContent:TextField;
		
		private var upBtn:ButtonUtils;
		
		private var inputFormat:TextFormat;
		
		private var smallBack:Shape;
		private var bigBack:Shape;
		
		//================
		private var shopInfoID:int = -999;
		private var _onComment:Function;
		
		private var type:int = 0;
		
		public function CommentPanel()
		{
			
		}
		
		public function init(stage:Stage, loader:ResourceLoader, lang:Lang,uri:URI = null):void
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;
			
			initThis();
		}
		
		private function initThis():void
		{
			titleText = new TextField();
			titleText.mouseEnabled = false;
			titleText.autoSize = TextFieldAutoSize.LEFT;
			this.addChild(titleText);
			titleText.y = _stage.stageHeight * .1;
			TextFieldUtils.setHtmlText(titleText,HtmlText.formatSizeColor("请轻触星级",0xffffff,26));
			titleText.x = (_stage.stageWidth - titleText.width) * 0.5;
			
			inputFormat = new TextFormat;
			inputFormat.bold = true;
			inputFormat.color = 0xFFFFFF;
			inputFormat.size = HtmlText.FONT_SIZE + 18;
			
			bigBack = new Shape;
			bigBack.graphics.lineStyle(1,0x000000,1);
			bigBack.graphics.beginFill(0x444444,.8);
			bigBack.graphics.drawRoundRect(0,0,_stage.stageWidth* .8,_stage.stageHeight * .4,5);
			bigBack.graphics.endFill();
			this.addChild(bigBack);
			bigBack.x =  (_stage.stageWidth - bigBack.width) * 0.5;
			bigBack.y = _stage.stageHeight * .36;
			
			commentContent = new TextField();
			commentContent.wordWrap = true;
			commentContent.multiline = true;
			commentContent.type = TextFieldType.INPUT;
			this.addChild(commentContent);
			commentContent.width = _stage.stageWidth - _stage.stageWidth * .2;
			commentContent.height = _stage.stageHeight * .4;
			commentContent.x = (_stage.stageWidth - commentContent.width) * 0.5;
			commentContent.y = _stage.stageHeight * .36;
			commentContent.addEventListener(TextEvent.TEXT_INPUT,changeStyleHandler);
			
			var commentTxt:TextField = new TextField();
			commentTxt.autoSize = "left";
			commentTxt.mouseEnabled = false;
			TextFieldUtils.setHtmlText(commentTxt,HtmlText.format("请在下面输入评价内容（可不填）",0xffffff,26));
			commentTxt.x = (_stage.stageWidth - commentTxt.width) * 0.5;
			commentTxt.y = commentContent.y - commentTxt.height;
			this.addChild(commentTxt);

			var btn:Bitmap = new _loader.uiManager.btnBack as Bitmap;
			btn.width = _stage.stageWidth * .25;
			btn.height = _stage.stageHeight * .05;
			
			upBtn = new ButtonUtils(btn,upThis,HtmlText.formatSizeColor("<b>" + "确认评论" + "</b>",0xFFFF00));
			upBtn.x = _stage.stageWidth * .375;
			upBtn.y = _stage.stageHeight * .8;
			this.addChild(upBtn);
			
			star = new StarGroup(StarGroup.WRITE_STARS);
			SizeManager.changeSizeFromWid(star,_stage.stageWidth * 0.7);
			star.x = (_stage.stageWidth - star.width) * 0.5;
			star.y = _stage.stageHeight * .16;
			this.addChild(star);
			
		}
		
		private function upThis():void
		{
			if(_onComment is Function){
				_onComment({value:commentContent.text,star:star.starLevel});
			}
		}
		
		protected function changeStyleHandler(e:TextEvent):void
		{
			setTimeout(function():void{e.target.setTextFormat(inputFormat);},10);
		}
		
		public function changeData(obj:Object):void
		{
			if(obj){
				shopInfoID = obj.id;
				TextFieldUtils.setHtmlText(titleText,HtmlText.formatSizeColor("<b>" + obj.name + "</b>",0xFFFF00,20),true,true);
				titleText.width = titleText.textWidth + 5;
				titleText.height = titleText.textHeight + 5;
			}
		}
		
		public function clear():void
		{
			if(_stage){
				shopInfoID = -999;
				type = 0;
				
				titleText = null;
				star = null;
				commentContent = null;
				upBtn = null;
				inputFormat = null;
				smallBack = null;
				bigBack = null;
				
				_stage = null;
				_loader = null;
				_lang = null;
			}
		}
		
		public function set comment(func:Function):void
		{
			_onComment = func;
		}
		
		public function set showType(thisType:int):void
		{
			type = thisType; 
		}
	}
}